package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.activity.softVersion.Config;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.req.base.BaseCommand;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 *  信息框-列表 异步请求
 */
public class GetSoftVersion extends BaseCommand {

    private String address;

    public GetSoftVersion() {
    }

    public GetSoftVersion(String address) {
        this.address = address;
    }

    @Override
    public String getRequestUrl() {
        return Config.UPDATE_SERVER;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        if (address != null){
            Log.d("android","111111111");
            paramters.put("action",address);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetMessageInfoLIst.yes()");
        try {
            JSONObject object = new JSONObject(result.object.toString());
            JSONObject o = new JSONObject(object.getString("data"));
            String android_address = null;
            String version = null;
            if (address != null){
                android_address = o.getString("android_address");
                Log.d("android","1111111112222222");
            }else {
                Log.d("android","222222222");
                version = o.getString("android");
            }
            DataCenter.version = version;
            DataCenter.address = android_address;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_SOFTVERSION_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetMessageInfoLIst.no()");

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_SOFTVERSION_FAILED);

    }
}
